import {autoAddDictionary} from '~/plugins/Tools/autoStore';
import { SetToken, UnsetToken, DeepCopy } from '~/plugins/Tools';
import Config from '~/config';
let {Local} = Config;

const dictionary = { lx: [], xb: [] };

const __state = {
  user: {
    userAccount: '',
    nickname: '',
    role: '',
    userPassword: '',
    photo: '',
  },
  hz: [],
  custom: {
    id: '',
    name: '',
  },
  menu: [],
  app: {
    id: '',
    aName: '',
    aCode: '',
  },
  auth: [],
  authUser: false,
};

export const state = () => ({
  ...__state,
  ...autoAddDictionary('state', dictionary),
});

/**
 * USER/SET
 * USER/DEL
 *
 * @type {{SET: mutations.SET, DEL: mutations.DEL}}
 */

export const mutations = {
  SET: function (state, payload) {
    if (!payload) {
      state.authUser = false;
      return;
    }
    state.authUser = true;
    let {auth, user, custom, menu, app, hz} = payload;
    state.custom = custom;
    state.user = user;
    state.hz = hz;
    state.menu = menu;
    state.app = app;
    state.auth = auth;
    SetToken(payload, Local.name);
  },
  SET_user: function (state, payload) {
    let {email, mobile} = payload;
    state.user.email = email;
    state.user.mobile = mobile;
  },
  SET_photo: function (state, payload) {
    state.user.photo = payload;
  },
  SET_hz: function (state, payload) {
    state.hz[0] = payload;
  },
  DEL: function (state) {
    for (let index in __state) {
      if (__state.hasOwnProperty(index)) {
        if (__state[index].constructor === Object) {
          state[index] = Object.assign(__state[index]);
        } else if (__state[index].constructor === Array) {
          state[index] = DeepCopy(__state[index]);
        }
      }
    }
    UnsetToken();
  },
  ...autoAddDictionary('mutations', dictionary),
};

export const getters = {};

export const actions = {

};
